/*
 * Copyright 2007-2009 Thomas Krause (thomas@berlin-reloaded.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gidoo.cms.helper;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.wicket.util.resource.AbstractResourceStreamWriter;
import org.apache.wicket.util.time.Time;
import org.gidoo.cms.GidooCMS;

/**
 * Streams a resource.
 * @author <a href="mailto:thomas@berlin-reloaded.de>Thomas Krause</a>
 */
public class CMSResourceStreamWriter extends AbstractResourceStreamWriter
{
  
  private InputStream dataIn;
  private String mime;
  private long lastModified;
  private long size;
  
  /**
   * Constructor.
   * @param resource The resource to stream.
   * @param app The GidooCMS WebApplication.
   */
  public CMSResourceStreamWriter(Node resource, GidooCMS app)
  {
    if(resource == null || app == null)
    {
      throw new NullPointerException();
    }
    
    try
    {
      if(! "cms:resource".equals(resource.getPrimaryNodeType().getName()))
      {
        throw new IllegalArgumentException("resource param node needs to be of type \"cms:resource\"");
      }

      dataIn = resource.getProperty("jcr:data").getStream();
      mime = resource.getProperty("jcr:mimeType").getString();
      if(resource.hasProperty("jcr:lastModified"))
      {
        lastModified = resource.getProperty("jcr:lastModified").getDate().getTimeInMillis();
      }
      else
      {
        lastModified = new Date().getTime();
      }
      if(resource.hasProperty("cms:size"))
      {
        size = resource.getProperty("cms:size").getLong();
      }
      else
      {
        size = -1;
      }
    }
    catch(RepositoryException ex)
    {
      Logger.getAnonymousLogger().log(Level.SEVERE, "could not read resource stream", ex);
    }
  }

  public String getContentType()
  {
    return mime;
  }

  @Override
  public long length()
  {
    return this.size;
  }

  @Override
  public Time lastModifiedTime()
  {
    return Time.valueOf(lastModified);
  }

  public void write(OutputStream output)
  {
    if(dataIn != null)
    {
      try
      {
        BufferedInputStream in = new BufferedInputStream(dataIn);
        int b = in.read();
        while(b > -1)
        {
          output.write(b);
          b = in.read();
        }
        in.close();
      }
      catch (IOException ex)
      {
        Logger.getLogger(CMSResourceStreamWriter.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }
}
